//
//  StotramController.m
//  TabNav
//
//  Created by Kalyan Mudumbai on 8/28/10.
//  Copyright 2010 GOLDMAN SACHS. All rights reserved.
//

#import "StotramController.h"
#import "CustomCell.h"
#import "SlokamController.h"

@implementation StotramController
@synthesize slokas;
@synthesize stotramBySections;
@synthesize selectedLanguage;

-(void) viewDidLoad {
	
	NSString * fileName = [[NSString alloc] init];
	
	if([selectedLanguage isEqual:@"Telugu"]) {
		fileName = @"Telugu-Full";
	} else if ([selectedLanguage isEqual:@"English"]) {
		fileName = @"English-Full";
	}
	
	//load the slokas
	NSBundle * bundle = [NSBundle mainBundle];
//	NSString * plistPath = [bundle pathForResource:@"Telugu-Full" ofType:@"plist"];
	NSString * plistPath = [bundle pathForResource:fileName ofType:@"plist"];
	NSLog(@"Stotram path is: %@", plistPath);
//	slokas = [[NSArray alloc] initWithContentsOfFile:plistPath];
	stotramBySections = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
	
}

-(void) viewDidUnload {
//	self.slokas = nil;
	self.stotramBySections = nil;
	self.selectedLanguage = nil;
	[super viewDidUnload];
}

-(void) dealloc {
//	[slokas release];
	[stotramBySections release];
	[selectedLanguage release];
	[super dealloc];
}

#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
//    return 1;
	return [[stotramBySections allKeys] count];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
	if(section == 0) {
		return [(NSArray*)[stotramBySections objectForKey:@"Guru Parampara"] count];
	}
	else if(section == 1) {
		return [(NSArray*)[stotramBySections objectForKey:@"Lakshmi Astto:tharam"] count];
	}
	else if(section == 2) {
		return [(NSArray*)[stotramBySections objectForKey:@"Sri Vishnu Sahasrana:mam"] count];
	}
	
    return [slokas count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
	static NSString * customCellIdentifier = @"CustomCellIdentifier";
	NSString * nibName = @"CustomCell";
	
	if([selectedLanguage isEqual:@"English"]) {
		nibName = @"LargeCustomCell";
	}
	
	CustomCell * cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:customCellIdentifier];
	if(cell == nil) {
		NSArray * nib = [[NSBundle mainBundle] loadNibNamed:nibName owner:self options:nil];
		for (id oneObject in nib){
			if ([oneObject isKindOfClass: [CustomCell class]]) {
				cell = (CustomCell * ) oneObject;
			}
		}
	}
	
	NSUInteger section = [indexPath section];
	if(section == 0) {
		slokas = (NSArray*)[stotramBySections objectForKey:@"Guru Parampara"];
	}
	else if(section == 1) {
		slokas = (NSArray*)[stotramBySections objectForKey:@"Lakshmi Astto:tharam"];
	}
	else if(section == 2) {
		slokas = (NSArray*)[stotramBySections objectForKey:@"Sri Vishnu Sahasrana:mam"];
	}
	
	NSDictionary * slokam = (NSDictionary*)[slokas objectAtIndex:[indexPath row]];
	
	if([selectedLanguage isEqual:@"Telugu"]) {
		cell.firstLine.font = [UIFont fontWithName:@"TeluguSangamMN-Bold" size:16];
		cell.secondLine.font = [UIFont fontWithName:@"TeluguSangamMN-Bold" size:16];
	}

	cell.firstLine.text = [slokam objectForKey:@"f"];
	cell.secondLine.text = [slokam objectForKey:@"s"];
	
	return cell;
	
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
	NSLog(@"Coloring cell %d",[indexPath row]);
	//UIColor *aColor = [UIColor colorWithRed:0.367 green:0.504 blue:0.681 alpha:1.000];
	// http://cloford.com/resources/colours/500col.htm
	
    if (indexPath.row == 0 || indexPath.row%2 == 0) {
        //UIColor *altCellColor = [UIColor colorWithWhite:0.7 alpha:0.1];
		//UIColor *altCellColor = [UIColor lightGrayColor];
		//UIColor * altCellColor = [UIColor orangeColor];
		UIColor * altCellColor = [UIColor colorWithRed:(255/255) green:(182/255) blue:(193/255) alpha:0.1];
		CustomCell * cc = (CustomCell*)cell;
		//cc.cView.alpha = 1.0;
		cc.cView.backgroundColor = altCellColor;
    } else {
//		UIColor * altCellColor = [UIColor whiteColor];
		UIColor * altCellColor = [UIColor colorWithRed:(245/255) green:(222/255) blue:(179/255) alpha:0.1];		
		CustomCell * cc = (CustomCell*)cell;
		//cc.cView.alpha = 1.0;
		cc.cView.backgroundColor = altCellColor;
	}
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
	if([selectedLanguage isEqual:@"English"]) {
		return 100;
	}
	return kTableViewRowHeight;
}

#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	
	// Vishnu Sahasram is in the 3rd section (therefore section number is 2)
	if([indexPath section] != 2) {
		return;
	}
	
	NSUInteger selectedRow = [indexPath row];
	NSArray * sArray = (NSArray*)[stotramBySections objectForKey:@"Sri Vishnu Sahasrana:mam"];
	NSDictionary * slokam = [sArray objectAtIndex:selectedRow];
	BOOL isSelectable = [[slokam objectForKey:@"select"] boolValue];
	NSLog(@"Selectable value for row %d is %d",selectedRow, isSelectable);
	if(!isSelectable) {
		NSLog(@"Cell at %d is not selectable",selectedRow);
		return;
	}
	
    // Navigation logic may go here. Create and push another view controller.
	NSLog(@"Selected slokam is: %@",[sArray objectAtIndex:[indexPath row]]);
	SlokamController * sc = [[SlokamController alloc] init];
	
	// subtracting 1 because the sloka count starts from 1, but table count starts from 0 
	sc.selectedSlokamNumber = ([[slokam objectForKey:@"no"] integerValue]-1);
	[self.navigationController pushViewController:sc animated:YES];
	[sc release];

}

- (id)initWithStyle:(UITableViewStyle)style
{
	if (self = [super initWithStyle:UITableViewStyleGrouped])
	{
	}
	return self;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
	if(section == 0) {
		return @"Guru Parampara";
	}
	else if(section == 1) {
		return @"Lakshmi Astto:tharam";
	}
	else if(section == 2) {
		return @"Sri Vishnu Sahasrana:mam";
	}	
	
	return @"None";
}

@end
